//
//  MemberfindIntegralListViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/26.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//

#import "MemberfindIntegralListViewController.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "UIImageView+WebCache.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"
@interface MemberfindIntegralListViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UISearchBarDelegate>
@end

@implementation MemberfindIntegralListViewController

{
    UISearchBar *search;
    MJRefreshHeaderView* _header;
    MJRefreshFooterView* _footer;
    NSMutableArray* createDate;
    NSMutableArray* integral;
    NSMutableArray* memo;
    BOOL saveAccount;
    MBProgressHUD *progressHud;
    NSString*TotalCount;
    UITableView *table;
    
}
@synthesize alt = alt;

-(void)BackAction:(UIButton *)sender{
    
    
    [self.navigationController popViewControllerAnimated:YES];

}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];

    [self setnavi];
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];

}
-(void)requestData{

    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"1" forKey:@"pageIndex"];
    [param setValue:@"20" forKey:@"pageSize"];
    [param setValue:self.memberId forKey:@"memberId"];
    [HttpRequest requestData:@"Business/Member/findIntegralList" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                NSMutableArray* arrr=[[NSMutableArray alloc]init];
                
                arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                TotalCount=[severDataDict.dataDict objectForKey:@"totalIntegral"];
                UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,90/2)];
                view.backgroundColor= zuiqianhuiColor;
                 table.tableHeaderView=view;
                
                UILabel*integrals=[[UILabel alloc]initWithFrame:CGRectMake(17, 0, ScreenWidth, 45)];
                integrals.text=@"当前积分";
                integrals.font=[UIFont systemFontOfSize:15];
                [view addSubview:integrals];
                UILabel*TotalCounts=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-200, 0, 185, 45)];
                TotalCounts.text=[NSString stringWithFormat:@"%@",TotalCount];
                TotalCounts.textAlignment=NSTextAlignmentRight;
                TotalCounts.textColor=hongseColor;
                TotalCounts.font=[UIFont boldSystemFontOfSize:18];
                [view addSubview:TotalCounts];
                
                createDate= [[NSMutableArray alloc] init];
                integral= [[NSMutableArray alloc] init];
                memo= [[NSMutableArray alloc] init];
        
                for (NSDictionary* dicc in arrr )
                {
                    [memo addObject:[dicc valueForKey:@"memo"]];
                    [integral addObject:[dicc valueForKey:@"integral"]];
                    [createDate addObject:[dicc valueForKey:@"createDate"]];
                    
                }
                [table reloadData];
                
                
                
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
    
    
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [integral count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
   
    NSRange range={0,10};
    NSString* string =[[createDate objectAtIndex:indexPath.row] substringWithRange:range];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy.MM.dd"];
    NSDate *newsDateFormatted = [dateFormatter dateFromString:string];
    NSString *str= [dateFormatter stringFromDate:newsDateFormatted];
    UIImage *img=[UIImage imageNamed:@"jifenyuanquan.png"];
    UIImage *tinted = [img rt_tintedImageWithColor:luseColor ];
    UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(15+10,0, 1,90)];
    line.backgroundColor=luseColor;
    [cell.contentView addSubview:line];
    UIImageView*imageV=[[UIImageView alloc]initWithFrame:CGRectMake(15, 35, 20, 20)];
    imageV.image=tinted;
    [cell.contentView addSubview:imageV];
    UILabel*titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(imageV.right+15, 27, ScreenWidth-110,15)];
    titleLabel.text =[NSString stringWithFormat:@"%@",  [memo objectAtIndex:indexPath.row]];
    titleLabel.font=[UIFont systemFontOfSize:14];
    titleLabel.numberOfLines=0;
    [cell.contentView addSubview:titleLabel];
    UILabel*timeLabel=[[UILabel alloc]initWithFrame:CGRectMake(imageV.right+15, 50, 95,13)];
    timeLabel.text =[NSString stringWithFormat:@"%@", str];
    timeLabel.font=[UIFont systemFontOfSize:13];
    timeLabel.textColor=zhonghuiColor;
    [cell.contentView addSubview:timeLabel];
    UILabel*priceLabel=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-80, 0, 65,90)];
    priceLabel.text =[NSString stringWithFormat:@"%@",  [integral objectAtIndex:indexPath.row]];
    priceLabel.font=[UIFont systemFontOfSize:16];
    priceLabel.textAlignment=NSTextAlignmentRight;
    [cell.contentView addSubview:priceLabel];
    UIImageView*imagexi=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth-80, 20, 1, 50)];
    imagexi.image=[UIImage imageNamed: @"jifenmingxi_xixian.png"];
    [cell.contentView addSubview:imagexi];


    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 90;
}

#pragma mark - Table view delegate

- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
-(void)setnavi{
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
    self.title=@"积分明细";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    [self requestData];
    
}


@end

